package io.gitee.cloudladder.obsidian.swagger2.plugin;

import io.gitee.cloudladder.obsidian.swagger2.annotation.ApiGuideImage;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import springfox.documentation.service.StringVendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;

import java.util.Arrays;
import java.util.Objects;

@Order(Ordered.HIGHEST_PRECEDENCE + 1000)
public class ApiGuideImageBuilderPlugin implements OperationBuilderPlugin {

    @Override
    public void apply(OperationContext operationContext) {
        ApiGuideImage apiGuideImage = operationContext.getHandlerMethod().getMethodAnnotation(ApiGuideImage.class);
        if (Objects.nonNull(apiGuideImage)) {
            String image =  apiGuideImage.value();
            operationContext.operationBuilder().extensions(Arrays.asList(new StringVendorExtension("x-guide-image", String.valueOf(image))));
        }
    }

    @Override
    public boolean supports(DocumentationType documentationType) {
        return true;
    }

}
